import money from '@/api/money'

const state = {
	month_total: 0,
	percentage: 0,
	remain: 0,
	return: 0,
	share: 0,
	total: 0,
	revenueHistory:{
		data: [],
		noData: false,
		loading: false,
		allLoaded: false
	},
	revenueMonthly:{
		data: [],
		noData: false,
		loading: false,
		allLoaded: false
	},
	lastMonth: {
		flag: 0,
	},
	requestSend: {
		getMoneyOverview: false,
		getLastMonth: false
	}
}

const getters = {
	moneyData: state => state,
	thisMonth: () => new Date().getMonth() + 1,
	lastMonth: state => state.lastMonth,
	revenueHistory: state => state.revenueHistory,
	revenueMonthly: state => state.revenueMonthly
}

const actions = {
	async getMoneyOverview({state, commit}){
		if(state.requestSend.getMoneyOverview) return
		const {data, success} = await money.getIncome()
		commit('moneyOverview', data)
		commit('requestComplete', 'getMoneyOverview')
	},
	async getLastMonth({state, commit}){
		if(state.requestSend.getLastMonth) return state.lastMonth
		const {data, success} = await money.getLastMonth()
		commit('moneyLastMonth', data)
		commit('requestComplete', 'moneyLastMonth')
	},
	async drawMoney({commit}, amount) {
		const {data, success, msg} = await money.drawMoney(amount)
		if(success) commit('recalculateMoney', amount)
		return {success, msg}
	},
	// 历史收益明细
	async getRevenueHistory({commit}, {type, page}) {
		commit('multiplePageRequest', {name: 'revenueHistory', ajaxing: true})
		const {data, success} = await money.getRevenueHistory({type, page})
		commit('revenueHistory', data)
		commit('multiplePageRequest', {name: 'revenueHistory', ajaxing: false})
		return success
	},
	// 月收益列表
	async getRevenueMonthly({commit}, {page}) {
		commit('multiplePageRequest', {name: 'revenueMonthly', ajaxing: true})
		const {data, success} = await money.getRevenueMonthly({page})
		commit('revenueMonthly', data)
		commit('multiplePageRequest', {name: 'revenueMonthly', ajaxing: false})
		return success
	},
}

const mutations = {
	requestComplete(state, item){
		state.requestSend[item] = true
	},
	multiplePageRequest(state, {name, ajaxing}) {
		state[name].loading = ajaxing
	},
	moneyOverview(state, data){
		state.month_total = data.month_total
		state.percentage = data.percentage
		state.remain = data.remain
		state.return = data.return
		state.share = data.share
		state.total = data.total
	},
	moneyLastMonth(state, data){
		state.lastMonth = data
	},
	recalculateMoney(state, amount) {
		state.remain = state.remain - amount
	},
	revenueHistory(state, data) {
		const dataWithMonth = data.map(d => ({
				month: new Date().getMonth(d.created_at) + 1,
				year: new Date().getFullYear(d.created_at),
				...d
			})
		)
		const rev = state.revenueHistory
		if(!data.length){
			if(!rev.data.length) rev.noData = true
			else rev.allLoaded = true
		}
		rev.data = [...rev.data, ...dataWithMonth]
	},
	clearRevenueHistory(state) {
		const rev = state.revenueHistory
		rev.data = []
		rev.noData = false
		rev.loading = false
		rev.allLoaded = false
	},
	revenueMonthly(state, data) {
		const rev = state.revenueMonthly
		if(!data.length){
			if(!rev.data.length) rev.noData = true
			else rev.allLoaded = true
		}
		rev.data = data
	},
}

export default {
  state,
  getters,
  actions,
  mutations
}

